package com.lzb.findphone.fragment;

import android.media.AudioManager;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.VolumeUtils;
import com.lzb.findphone.BaseNoModelFragment;
import com.lzb.findphone.R;
import com.lzb.findphone.bean.SettingBean;
import com.lzb.findphone.databinding.FragmentFlashlightBinding;
import com.lzb.findphone.utils.AppSpUtils;
import com.lzb.findphone.utils.FlashlightUtils;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FlashlightFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FlashlightFragment extends BaseNoModelFragment<FragmentFlashlightBinding> implements View.OnClickListener {

    private float openTime, closeTime;
    private int min = 0, max = 6, openProgress, closeProgress;
    private Handler flashHandler;
    private Runnable flashRunnable;
    private boolean isFlashing = false;

    @Override
    protected int onCreate() {
        return R.layout.fragment_flashlight;
    }

    @Override
    protected void initView() {
        mDataBinding.tvApply.setOnClickListener(this);
        mDataBinding.ivOpenTimeMin.setOnClickListener(this);
        mDataBinding.ivOpenTimeAdd.setOnClickListener(this);
        mDataBinding.ivCloseTimeMin.setOnClickListener(this);
        mDataBinding.ivCloseTimeAdd.setOnClickListener(this);
        mDataBinding.tvTest.setOnClickListener(this);

        flashHandler = new Handler();

        mDataBinding.sbOpenTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                openProgress = i;
                openTime = calSeekBarRealValue(mDataBinding.sbOpenTime, i);
                mDataBinding.tvOpenTime.setText(String.format("%.1fs", openTime));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mDataBinding.sbCloseTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                closeProgress = i;
                closeTime = calSeekBarRealValue(mDataBinding.sbCloseTime, i);
                mDataBinding.tvCloseTime.setText(String.format("%.1fs", closeTime));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvApply) {//应用
            SettingBean settingBean = AppSpUtils.getSetting();
            settingBean.setFlashlight(true);
            settingBean.setOpenTime((long) (openTime * 1000L));
            settingBean.setCloseTime((long) (closeTime * 1000L));
            AppSpUtils.saveSetting(settingBean);
            ToastUtils.showShort(getString(R.string.apply_suc));
        } else if (id == R.id.ivOpenTimeMin) {//开启灯光时间-
            if (openProgress > 0) {
                openProgress--;
                mDataBinding.sbOpenTime.setProgress(openProgress);
                openTime = calSeekBarRealValue(mDataBinding.sbOpenTime, openProgress);
                mDataBinding.tvOpenTime.setText(String.format("%.1fs", openTime));
            }
        } else if (id == R.id.ivOpenTimeAdd) {//开启灯光时间+
            if (openProgress < 6) {
                openProgress++;
                mDataBinding.sbOpenTime.setProgress(openProgress);
                openTime = calSeekBarRealValue(mDataBinding.sbOpenTime, openProgress);
                mDataBinding.tvOpenTime.setText(String.format("%.1fs", openTime));
            }
        } else if (id == R.id.ivCloseTimeMin) {//关闭灯光时间-
            if (closeProgress > 0) {
                closeProgress--;
                mDataBinding.sbCloseTime.setProgress(closeProgress);
                closeTime = calSeekBarRealValue(mDataBinding.sbCloseTime, closeProgress);
                mDataBinding.tvCloseTime.setText(String.format("%.1fs", closeTime));
            }
        } else if (id == R.id.ivCloseTimeAdd) {//关闭灯光时间+
            if (closeTime < 6) {
                closeProgress++;
                mDataBinding.sbCloseTime.setProgress(closeProgress);
                closeTime = calSeekBarRealValue(mDataBinding.sbCloseTime, closeProgress);
                mDataBinding.tvCloseTime.setText(String.format("%.1fs", closeTime));
            }
        } else if (id == R.id.tvTest) {//测试
            if (isFlashing) return;
            startFlashing();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    stopFlashing();
                }
            }, 12 * 1000L);
        }
    }

    @Override
    protected void initData() {
        SettingBean settingBean = AppSpUtils.getSetting();
        openTime = settingBean.getOpenTime() * 1f / 1000;
        closeTime = settingBean.getCloseTime() * 1f / 1000;
        openProgress = (int) (openTime * 60 / (max - min));
        closeProgress = (int) (closeTime * 60 / (max - min));
        mDataBinding.tvOpenTime.setText(String.format("%.1fs", openTime));
        mDataBinding.tvCloseTime.setText(String.format("%.1fs", closeTime));
        mDataBinding.sbOpenTime.setProgress(openProgress);
        mDataBinding.sbCloseTime.setProgress(closeProgress);
        FlashlightUtils.getInstance().init(mContext);
    }

    private void startFlashing() {
        if (isFlashing) return;

        isFlashing = true;

        flashRunnable = new Runnable() {
            @Override
            public void run() {
                if (!isFlashing) return;

                FlashlightUtils.getInstance().startFlash();

                flashHandler.postDelayed(() -> {
                    if (!isFlashing) return;

                    FlashlightUtils.getInstance().stopFlash();

                    if (isFlashing) {
                        flashHandler.postDelayed(this, (long) (closeTime * 1000L));
                    }
                }, (long) (openTime * 1000L));
            }
        };

        flashHandler.post(flashRunnable);
    }

    private void stopFlashing() {
        isFlashing = false;
        if (flashHandler != null && flashRunnable != null) {
            flashHandler.removeCallbacks(flashRunnable);
        }
        FlashlightUtils.getInstance().stopFlash();
    }

    private float calSeekBarRealValue(SeekBar seekBar, int progress) {
        int progressRange = seekBar.getMax();
        float value = progress * 1.0f / progressRange * (max - min) + min;
        return value;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isFlashing) {
            stopFlashing();
        }
    }
}